package com.wus.common.mybatis.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.wus.common.core.factory.YmlPropertySourceFactory;
import com.wus.common.mybatis.handler.MyMetaObjectHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * @Description: mybatis配置
 * @Author: wus
 * @Date: 2025/01/21 11:36
 */
@PropertySource(value = "classpath:common-mybatis.yaml", factory = YmlPropertySourceFactory.class)
@AutoConfiguration
@RequiredArgsConstructor
public class MybatisPlusConfig {

    /**
     * mybatis-plus 分页插件
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor paginationInnerInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }

    /**
     * 使用网卡信息绑定雪花生成器（低版本用不了）
     * 防止集群雪花ID重复
     */
    /*@Bean
    public IdentifierGenerator idGenerator() {
        return new DefaultIdentifierGenerator(NetUtil.getLocalhost());
        //  业务代码中，雪花唯一ID生成调用方式 -> identifierGenerator.nextId(null).toString();
    }*/

    @Bean
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }
}
